package com.inphase.myfood.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

public class SessionContext {
	private static SessionContext instance;
	private Map<String, HttpSession> sessionMap;

	private SessionContext() {
		sessionMap = new HashMap<String, HttpSession>();
	}

	public static SessionContext getInstance() {
		if (instance == null) {
			instance = new SessionContext();
		}
		return instance;
	}
	
	public static String getUserAccount(String sessionId){
		return (String) getInstance().getSession(sessionId).getAttribute("userAccount");
	}

	public static boolean validationSession(String sessionId) {
		HttpSession session = SessionContext.getInstance()
				.getSession(sessionId);
		if (null != session) {
			return true;
		} else {
			return false;
		}
	}

	public synchronized void addSession(HttpSession session) {
		if (session != null) {
			System.out.println("创建sessionId>>>>" + session.getId());
			sessionMap.put(session.getId(), session);
		}
	}

	public synchronized void delSession(HttpSession session) {
		if (session != null) {
			sessionMap.remove(session.getId());
		}
	}

	public synchronized HttpSession getSession(String sessionId) {
		if (sessionId == null)
			return null;
		return sessionMap.get(sessionId);
	}
}
